import {Component, OnInit,TemplateRef, OnDestroy} from '@angular/core';
import {NormalService} from "../../../apiServices/normal.service";
import {Subscription} from 'rxjs/Subscription';
import {Router} from '@angular/router';
import { BsModalService } from 'ngx-bootstrap/modal';
import { BsModalRef } from 'ngx-bootstrap/modal/bs-modal-ref.service';

@Component({
    selector: 'modify-password',
    templateUrl: './modify-password.template.html'
})
export class ModifyPasswordComponent implements OnInit, OnDestroy{
    public language: string = 'en';
    public userSubscription: Subscription;
    public logoutSubscription: Subscription;
    public user: any;
    public passwordModel: any = {
        id: '',
        oldpass: '',
        newpass: '',
        repass: ''
    };
    passValid: boolean = true;

    modalRef: BsModalRef;
    isLoginMess: any = {en: 'The user is not logged in', zh: '用户没有登录！'};
    passIssame: any = {en: 'The passwords entered twice do not match !', zh: '两次密码输入不匹配'};

    canGetEmail: boolean = true;
    getEmailName: string = 'submit';
    sendEmailError: boolean = false;
    
   
    openModal(template: TemplateRef<any>) {
      this.modalRef = this.modalService.show(template);
    }
    constructor(
        private modalService: BsModalService,
        private normalService: NormalService,
        private router: Router
    ) {
        this.language = normalService.language;
        if(normalService.isLogin()){
            normalService.getuser().then().catch((error) => {
                alert(this.normalService.getErrors(error));
            });
        }
        this.userSubscription = normalService.user$.subscribe(user=>{
            this.passwordModel.id = user._id;
            this.user = user;
        });
        this.logoutSubscription = normalService.logout$.subscribe(logout => {
            if (logout == true ){
                if(this.normalService.lang == 'en'){
                    alert(this.isLoginMess.en);
                }
                if(this.normalService.lang == 'zh'){
                    alert(this.isLoginMess.zh);
                } 
                this.router.navigate(["/"]);
            }
                
        });
        
        if(this.normalService.lang == 'en'){
            this.getEmailName = 'submit';
        }else if(this.normalService.lang == 'zh'){
            this.getEmailName = '提交';
        }
    }
    submitPass(): void {
        console.log(this.passwordModel);
        if (this.passwordModel.newpass != this.passwordModel.repass){
            if(this.normalService.lang == 'en'){
                alert(this.passIssame.en);
            }
            if(this.normalService.lang == 'zh'){
                alert(this.passIssame.zh);
            }
        }
        this.normalService.updatepassword(this.passwordModel.id, {oldpass: this.passwordModel.oldpass, newpass: this.passwordModel.newpass}).then(suc => {
            this.normalService.loginOut();
        }).catch(error=>{
            alert(this.normalService.getErrors(error));
        })
    }

    forgetPassword(email: string): void{
        this.canGetEmail = false;
        if(this.normalService.lang == 'en'){
            this.getEmailName = 'sending...';
        }else if(this.normalService.lang == 'zh'){
            this.getEmailName = '发送中...';
        }
        this.normalService.forgetPassword(email, this.normalService.lang).then(result => {
            console.log('result',result);
            if(result && result.code && result.code ===200){
                alert(this.normalService.getSucc(result.message));
                this.sendEmailError = false;
                this.canGetEmail = false;
                let count = 120;
                this.getEmailName = count.toString();
                let id = setInterval(() => {
                    --count;
                    if(count === 0){
                        clearInterval(id);
                        if(this.normalService.lang == 'en'){
                            this.getEmailName = 'submit';
                        }else if(this.normalService.lang == 'zh'){
                            this.getEmailName = '提交';
                        }
                        this.canGetEmail = true;
                    }else{
                        this.getEmailName = count.toString();
                    }
                },1000);
            }else{
                this.sendEmailError = false;
                this.canGetEmail = true;
                if(this.normalService.lang == 'en'){
                    this.getEmailName = 'submit';
                }else if(this.normalService.lang == 'zh'){
                    this.getEmailName = '提交';
                }
            }
            //alert(result.info);
        }).catch(error => {
            this.sendEmailError = false;
                this.canGetEmail = true;
                if(this.normalService.lang == 'en'){
                    this.getEmailName = 'submit';
                }else if(this.normalService.lang == 'zh'){
                    this.getEmailName = '提交';
                }
            alert(this.normalService.getErrors(error));
        })
    }
    isSame(){
        if (this.passwordModel.newpass != this.passwordModel.repass){
            this.passValid = false;
        } else{
            this.passValid = true;
        }
    }
    ngOnInit(): void {
        
    }
    ngOnDestroy(): void {
        this.userSubscription.unsubscribe();
        this.logoutSubscription.unsubscribe();
    }
}